import { Sequence } from './Sequence'
import { EventEmitter } from 'events'
import { existsSync } from 'fs-extra'
import { PROJECT_ITEM_TYPE } from './enums'
import { ProjectItem } from './ProjectItem'
import { HostType } from '../../../constants/common'
import { methodEvaler } from '../../../core/common'

const _evalMethod = methodEvaler(HostType.pr)
export class Project extends EventEmitter {
  name: string
  documentID: string
  path: string
  nodeId: string
  treePath: string
  type: PROJECT_ITEM_TYPE
  rootItem?: ProjectItem
  activeSequence?: Sequence
  constructor(item: any) {
    super()
    this.name = item.name
    this.documentID = item.documentID
    this.path = item.path
    this.nodeId = item.nodeId
    this.treePath = item.treePath
    this.type = item.type
    if (item.rootItem) {
      this.rootItem = new ProjectItem(item.rootItem)
    }
    if (item.activeSequence) {
      this.activeSequence = new Sequence(item.activeSequence)
    }
  }
  evalScript(method: string, ...args: any) {
    return _evalMethod(method, this.nodeId, ...args)
  }
  save() {
    return this.evalScript('save')
  }
  saveAs(path: string) {
    return this.evalScript('saveAs', path)
  }
}
